﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GLaDOSAtHomeCommunity;

namespace GLaDOSAtHomeCommunity
{
    public partial class MainForm : Form
    {
        private GameManager m_GameManager = null;
        private int m_SecondsLeft = 0;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            bool steamon = GameDetection.SteamConnect();

            this.c_GameAvailabilityList.Items.Clear();
            foreach (KeyValuePair<string, Game> kv in Program.Games)
                this.c_GameAvailabilityList.Items.Add(kv.Value);
            for (int i = 0; i < this.c_GameAvailabilityList.Items.Count; i++)
            {
                if (steamon)
                    this.c_GameAvailabilityList.SetItemChecked(i, GameDetection.SteamOwnsGame((UInt32)(this.c_GameAvailabilityList.Items[i] as Game).AppID));
                else
                    this.c_GameAvailabilityList.SetItemChecked(i, true);
            }
            this.m_GameManager = new GameManager(this);
            this.c_ARGSyncToggleButton.Enabled = true;

            if (steamon)
                GameDetection.SteamDisconnect();
        }

        private void c_ARGSyncToggleButton_Click(object sender, EventArgs e)
        {
            if (this.c_ARGSyncToggleButton.Text == "START")
            {
                this.c_ARGSyncToggleButton.Enabled = false;
                this.c_ARGSyncToggleButton.Text = "WAIT (30)";
                this.c_ARGSyncToggleButton.BackColor = Color.Yellow;
                this.m_GameManager.StartMonitoring();
            }
            else
            {
                this.c_ARGSyncToggleButton.Enabled = false;
                this.c_ARGSyncToggleButton.Text = "START";
                this.c_ARGSyncToggleButton.BackColor = Color.Lime;
                this.m_GameManager.StopMonitoring();
                this.c_CheckTimer.Stop();
                this.c_NextCheckInLabel.Text = "Next check in: -- not started --";
                this.m_SecondsLeft = 0;
                this.c_ARGSyncToggleButton.Enabled = true;
            }
        }

        public void UpdateStatistics()
        {
            if (this.m_GameManager.GlobalTarget == null)
                this.c_TargetLabel.Text = "-- unknown --";
            else
                this.c_TargetLabel.Text = Program.Games[this.m_GameManager.GlobalTarget].GameName;
            if (this.m_GameManager.CurrentRunning == null)
                this.c_RunningLabel.Text = "-- none --";
            else if (this.m_GameManager.IsRunning == false)
                this.c_RunningLabel.Text = "-- waiting --";
            else
                this.c_RunningLabel.Text = Program.Games[this.m_GameManager.CurrentRunning].GameName;
        }

        public bool HasGame(string key)
        {
            for (int i = 0; i < this.c_GameAvailabilityList.Items.Count; i++)
                if ((this.c_GameAvailabilityList.Items[i] as Game).Key == key)
                    return this.c_GameAvailabilityList.GetItemChecked(i);
            return false;
        }

        public void EnableStop()
        {
            this.c_ARGSyncToggleButton.Enabled = true;
            this.c_ARGSyncToggleButton.Text = "STOP";
            this.c_ARGSyncToggleButton.BackColor = Color.Red;
        }

        public void SetWaitTime(int i)
        {
            this.c_ARGSyncToggleButton.Text = "WAIT (" + i + ")";
        }

        public void ForceStop()
        {
            this.c_ARGSyncToggleButton.Enabled = false;
            this.c_ARGSyncToggleButton.Text = "START";
            this.c_ARGSyncToggleButton.BackColor = Color.Lime;
            this.m_GameManager.StopMonitoring();
            this.c_ARGSyncToggleButton.Enabled = true;
            MessageBox.Show(this, "Unable to start game.  Check that it is installed.", "GLaDOS@Home");
            this.UpdateStatistics();
            this.c_CheckTimer.Stop();
            this.c_NextCheckInLabel.Text = "Next check in: -- not started --";
            this.m_SecondsLeft = 0;
        }

        public void ResetCheckTime()
        {
            this.m_SecondsLeft = 60 * 10;
            this.c_CheckTimer.Stop();
            this.c_CheckTimer.Start();
        }

        public void UpdateServerContactStatus()
        {
            if (this.m_GameManager.ServerContacting != null)
                this.c_NextCheckInLabel.Text = "Contacting " + this.m_GameManager.ServerContacting + "...";
        }

        private void c_CheckTimer_Tick(object sender, EventArgs e)
        {
            this.m_SecondsLeft -= 1;
            if (this.m_GameManager.ServerContacting != null)
                this.c_NextCheckInLabel.Text = "Contacting " + this.m_GameManager.ServerContacting + "...";
            else if (this.m_SecondsLeft < 0)
            {
                this.c_NextCheckInLabel.Text = "Next check in: -- not started --";
                this.c_CheckTimer.Stop();
            }
            else
            {
                int mins = (int)Math.Floor((double)this.m_SecondsLeft / 60);
                int secs = (int)Math.Floor((double)this.m_SecondsLeft % 60);
                this.c_NextCheckInLabel.Text = "Next check in: " + mins.ToString("D2") + ":" + secs.ToString("D2");
            }
        }
    }
}
